class MigrateJobsSecurity < ActiveRecord::Migration

  class << self
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Data insert/delete methods
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def up
      init_roles
      add_acl_data
    end

    def down
      init_roles
      delete_acl_data
    end

    private
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # Private data insert/delete methods
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      def init_roles
        @@admin = Role.find(1)
        #@@user = Role.find(2)
      end

      def add_acl_data
        add_job_management_data
        add_public_data
      end

      def add_job_management_data
        sobj = SystemObject.create(:name => "Job Management")
        @@admin.add_system_object(sobj)

        SystemMethod.create(:name => "jobs/update", :system_object_id => sobj.id)
        SystemMethod.create(:name => "jobs/create", :system_object_id => sobj.id)
        SystemMethod.create(:name => "jobs/disable", :system_object_id => sobj.id)
        SystemMethod.create(:name => "jobs/enable", :system_object_id => sobj.id)
      end

      def add_public_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Public'")
        SystemMethod.create(:name => "jobs/list", :system_object_id => sobj.id)
        SystemMethod.create(:name => "jobs/show", :system_object_id => sobj.id)
        SystemMethod.create(:name => "jobs/view_pdf", :system_object_id => sobj.id)
      end

      def delete_acl_data
        delete_public_data
        delete_job_management_data
      end

      def delete_public_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Public'")
        SystemMethod.delete_all("system_object_id = #{sobj.id} and name = 'jobs/list'")
        SystemMethod.delete_all("system_object_id = #{sobj.id} and name = 'jobs/show'")
        SystemMethod.delete_all("system_object_id = #{sobj.id} and name = 'jobs/view_pdf'")
      end

      def delete_job_management_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Job Management'")
        @@admin.delete_system_object(sobj)
        sobj.destroy
      end

  end
end
